package com.maserb.monitor.service.JwReport.impl;

import com.maserb.monitor.common.CreateTimeUtils;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.JwReport.JwReportEntity;
import com.maserb.monitor.enums.JwReportIsExpiredEnum;
import com.maserb.monitor.repository.JwVisitReport.JwReportRepository;
import com.maserb.monitor.service.JwReport.JwReportService;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class JwReportServiceImpl extends JpaSpecificationServiceImpl<JwReportRepository,
        JwReportRepository, JwReportEntity, String>
        implements JwReportService {

    @Override
    public List<JwReportEntity> findByUuid(String uuid) {
        return this.repository.findByUuid(uuid);
    }

    @Override
    public Page<JwReportEntity> findAllByUuid(Pageable pageable, String uuid) {
        return this.findAll(new Specification<JwReportEntity>() {
            @Override
            public Predicate toPredicate(Root<JwReportEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathUuid = root.get("uuid");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(uuid)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathUuid, "%" + uuid + "%")));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }

    @Override
    public Page<JwReportEntity> findAll(Pageable pageable, String realName, String sfzh, String phoneNumber, String acceptableStatus) {
        return this.findAll(new Specification<JwReportEntity>() {
            @Override
            public Predicate toPredicate(Root<JwReportEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathRealName = root.get("realName");
                Path<String> pathSfzh = root.get("sfzh");
                Path<String> pathPhoneNumber = root.get("phoneNumber");
                Path<String> pathAcceptableStatus = root.get("acceptableStatus");
                Path<String> pathUuid = root.get("uuid");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(realName)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathRealName, "%" + realName + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(sfzh)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathSfzh, "%" + sfzh + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(phoneNumber)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathPhoneNumber, "%" + phoneNumber + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(acceptableStatus)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathAcceptableStatus, acceptableStatus)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }

    @Override
    public void deleteByAcceptanceTime(ArrayList<String> keys) {
//        keys.forEach(id -> this.repository.deleteById(id));
        //3个月前时间
        String dateLessMonths = CreateTimeUtils.getCreateTimeLessMonths(-3);
        for(String id : keys){
            JwReportEntity entity = this.findOne(id);
            if(null!=entity.getAcceptanceTime() && !"".equals(entity.getAcceptanceTime())){
                    if(CreateTimeUtils.compare(entity.getAcceptanceTime(),dateLessMonths)){
                        try {
                            throw new Exception("有未过期的信访，删除失败");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }else{
                        this.delete(id);
                    }


            }
        }
    }
}